package com.vgemv.jsutilitysdkdemo.module.ChooseApp;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.RelativeLayout;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.vgemv.jsutilitysdk.activity.ChooseApp.JsChooseAppActivity;
import com.vgemv.jsutilitysdk.geometry.JsGeometryManager;
import com.vgemv.jsutilitysdk.JsTextView.JsTextView;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemStyleDefault;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemView.JsMenuItemView;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemVm.JsMenuItemVm;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemVm.JsMenuItemVmDefault;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuView;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewAdapter;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewBasicCallback;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewVm;

import java.util.ArrayList;

public class ChooseAppTesterActivity extends AppCompatActivity {


    private static final String TAG = ChooseAppTesterActivity.class.getSimpleName();
    public Handler mHandler = new Handler(Looper.getMainLooper());

    private JsTextView mtv;
    private RelativeLayout mrl_root;


    private ActivityResultLauncher mActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
        @Override
        public void onActivityResult(ActivityResult result) {
            Intent data = result.getData();
            if(data != null) {
                String packageName = data.getStringExtra(JsChooseAppActivity.APP_PACKAGE_NAME);
                String info = String.format("choose app package name:%s", packageName);
                Log.v(TAG, info);
            }
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);



        RelativeLayout relativeLayout = new RelativeLayout(this);
        relativeLayout.setId(View.generateViewId());
        mrl_root = relativeLayout;
        setContentView(relativeLayout);

        {
            JsMenuView menuView = new JsMenuView(this);
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, JsGeometryManager.pixelFromDp(300));
            lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            mrl_root.addView(menuView, lp);

            final int itemSize = JsGeometryManager.pixelFromDp(80);

            menuView.mBasicCallback = new JsMenuViewBasicCallback() {
                @Override
                public JsMenuViewAdapter jsMenuViewSetAdapter(JsMenuView menuView) {
                    return null;
                }

                @Override
                public void jsMenuItemViewWillDisplay(RecyclerView.ViewHolder viewHolder, int position) {

                }

                @Override
                public Rect jsMenuViewSetItemOffset(JsMenuView view, int index) {
                    int spanCount = view.mMenuViewVm.mi_spanCount;
                    int hPadding = (JsGeometryManager.getScreenSizeInPixel().getWidth() - spanCount * itemSize) / (spanCount + 2);
                    int vPadding = JsGeometryManager.pixelFromDp(20);

                    Rect rect = new Rect();
                    if (index == 0) {
                        rect.top = vPadding;
                        rect.left = hPadding;
                        rect.right = hPadding;
                        rect.bottom = vPadding;
                    } else if (index == view.vms().size() - 1) {
                        rect.top = vPadding;
                        rect.left = hPadding;
                        rect.right = hPadding;
                        rect.bottom = vPadding;
                    } else {
                        rect.top = vPadding;
                        rect.left = hPadding;
                        rect.right = hPadding;
                        rect.bottom = vPadding;
                    }
                    return rect;

                }

                @Override
                public JsMenuViewVm jsMenuViewSetVm(JsMenuView f) {
                    JsMenuViewVm vm = new JsMenuViewVm();
                    vm.mi_spanCount = 4;
                    return vm;
                }

                @Override
                public JsMenuItemView jsMenuViewCreateItemView(JsMenuView f, Context context) {
                    return null;
                }

                @Override
                public void jsMenuViewAfterCreateView(JsMenuView fragment) {

                }

                @Override
                public ArrayList<JsMenuItemVm> jsMenuViewSetDatasource() {
                    ArrayList<JsMenuItemVm> models = new ArrayList<JsMenuItemVm>();

                    JsMenuItemStyleDefault style = new JsMenuItemStyleDefault();
                    style.mI_backgroundColor = Color.BLACK;
                    style.mI_titleTextColor = Color.WHITE;
                    style.mi_height = itemSize;
                    style.mi_width = itemSize;

                    {
                        JsMenuItemVmDefault vm = new JsMenuItemVmDefault(0, "ChooseApp", "", itemSize, itemSize);
                        vm.setStyle(style);
                        models.add(vm);
                    }
                    return models;
                }

                @Override
                public void jsMenuViewClickItem(JsMenuView fragmentAlpha, int i, JsMenuItemVm model) {
                    switch (i) {
                        case 0: {
                            Intent intent = new Intent(ChooseAppTesterActivity.this, JsChooseAppActivity.class);
//                            startActivityForResult(intent,1);
                            mActivityResultLauncher.launch(intent);
                            break;
                        }
                        default: {
                            break;
                        }
                    }
                }
            };
        }
    }

    public void showMessage(String message ) {
        if(mtv != null) {
            //获取最底层的view DecoverView.
            ViewGroup viewGroup =(ViewGroup)mtv.getRootView();
            if(viewGroup != null) {
                mrl_root.removeView(mtv);
            }
        }
        mtv = new JsTextView(ChooseAppTesterActivity.this);
        mtv.setBackgroundColor(Color.RED);
        mtv.setMaxWidth(800);
        mtv.setText(message);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mrl_root.addView(mtv);
    }

//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        String packageName = data.getStringExtra(JsChooseAppActivity.APP_PACKAGE_NAME);
//        String info = String.format("choose app package name:%s",packageName);
//        Log.v(TAG,info);
//    }
}

